package com.atguigu.tingshu.search.api;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "专辑详情管理")
@RestController
@RequestMapping("api/search/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class itemApiController {

	@Autowired
	private ItemService itemService;


	/**
	 * 专辑上架
	 * 讲专辑文档存放到es的albumInfo索引库中
	 * 该接口是我们后台端调用的
	 * TODO : 通过定时任务定时将数据库中的专辑文档同步到es中
	 */

	@Operation(summary = "专辑上架到es中")
	@PostMapping("/upper/{albumId}")
	public  Result albumSale(@PathVariable("albumId") Long albumId) {
		 boolean flag=itemService.albumOnSale(albumId);
		return Result.ok(flag);
	}

	@Operation(summary = "专辑从es中下架")
	@DeleteMapping("/offSale/{albumId}")
	public  Result AlbumOffSale(@PathVariable("albumId") Long albumId) {
		boolean flag=itemService.albumOffSale(albumId);
		return Result.ok(flag);
	}
	@Operation(summary = "批量上架专辑到es中")
	@PostMapping("/batch/upper")
	public  Result batchAlbumOnSale() {
		boolean flag=false;
		for (int i = 1; i <=1500; i++) {
			 flag=itemService.albumOnSale((long) i);
		}
		return Result.ok(flag);
	}
	@Operation(summary = "批量下架es中的专辑")
	@DeleteMapping("/batch/offSale")
	public  Result batchAlbumOffSale() {
		itemService.batchAlbumOffSale();
		return Result.ok();
	}

	//Request URL: http://localhost:8500/api/search/albumInfo/1413

	@Operation(summary = "根据专辑id查询专辑详情")
	@GetMapping("/{albumId}")
	public Result getAlbumDetail(@PathVariable("albumId") Long albumId){
		Map<String, Object> map= itemService.getAlbumDetail(albumId);
		return Result.ok(map);
	}

	/**
	 * 当下: 当前我们自己调用, 更新排行榜信息到redis
	 * TODO: 整合xxl-job之后,自动更新排行榜信息到redis
	 * @param
	 * @param
	 * @return
	 */
	@Operation(summary = "查询排行榜信息,预热更新到redis中")
	@PutMapping("/update/rank")
	public Result rankPreHeat(){
   	  Boolean falg= itemService.rankPreHeat();
		return Result.ok(falg);
	}

	//Request URL: http://localhost:8500/api/search/albumInfo/findRankingList/1/hotScore
	@Operation(summary = "查询排行榜信息")
	@GetMapping("/findRankingList/{c1Id}/{dimension}")
	public Result findRankingList(@PathVariable("c1Id") Long c1Id,
								  @PathVariable("dimension") String dimension) {

		List<AlbumInfoIndexVo> albumInfoIndexVoList=itemService.findRankingList(c1Id, dimension);
		return Result.ok(albumInfoIndexVoList);
	}
}

